//
//  MapDepotViewController.m
//  PerfectWorld
//
//  Created by dllo on 16/8/31.
//  Copyright © 2016年 LST. All rights reserved.
//  装修攻略

#import "MapDepotViewController.h"
#import "MapDepotTableViewCell.h"
#import "WYScrollView.h"
#import "AFNetworking.h"
#import "NetworkHandler.h"
#import "MapDepotModel.h"
#import "CycleBanners.h"
#import "MapCycleModel.h"
#import "MapUser.h"
#import "MapWebViewController.h"
#import "StrategyList.h"
#import "MJRefresh.h"
#import "LoginLeftViewController.h"
#import "After.h"
#import "In.h"
#import "Before.h"
#import "JSDropDownMenu.h"
#import "CycleType.h"
@interface MapDepotViewController ()<UITableViewDataSource, UITableViewDelegate, JSDropDownMenuDataSource, JSDropDownMenuDelegate>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)MapDepotModel *mapdepotModel;
@property (nonatomic, strong)MapCycleModel *cycleModel;
@property (nonatomic, assign)NSInteger page;
@property (nonatomic, strong)NSMutableArray *arr;
@property (nonatomic, strong)NSArray *titleArr;

@property (nonatomic, strong)NSNumber *Category;
@end

@implementation MapDepotViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
    image.image = [UIImage imageNamed:@"装修攻略"];
    self.navigationItem.titleView = image;
    _titleArr = @[@"装修前", @"装修中", @"装修2"];
    _Category = @0;
    
    JSDropDownMenu *menu = [[JSDropDownMenu alloc] initWithOrigin:CGPointMake(0, 0) andHeight:50];
    menu.indicatorColor = [UIColor colorWithRed:175.0f/255.0f green:175.0f/255.0f blue:175.0f/255.0f alpha:1.0];
    menu.separatorColor = [UIColor colorWithRed:210.0f/255.0f green:210.0f/255.0f blue:210.0f/255.0f alpha:1.0];
    menu.textColor = [UIColor colorWithRed:83.f/255.0f green:83.f/255.0f blue:83.f/255.0f alpha:1.0f];
    menu.dataSource = self;
    menu.delegate = self;
    [self.view addSubview:menu];
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    leftBtn.frame = CGRectMake(5 , 0, 30 , 30 );
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"头像"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(clickNavLeftBtn:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    [self creatCycleData];
    [self creatTableView];
    // 下拉刷新, 上拉加载
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 0;
        [self handleData];
    }];
    [_tableView.mj_header beginRefreshing];
    _tableView.mj_footer = [MJRefreshBackFooter footerWithRefreshingBlock:^{
        [self creatMJRefreshFoot];
    }];

}
- (void)handleData {
    NSString *urlStr = [NSString stringWithFormat:@"http://api.guju.com.cn/v2/strategy/strategyList?userId=0&count=10&start=%ld&orderString=CATEGORY&category=%@", (long)_page, _Category];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:urlStr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = [NSDictionary dictionaryWithDictionary:responseObject];
        self.mapdepotModel = [MapDepotModel modelWithDic:dic];
        _arr = [NSMutableArray arrayWithArray:_mapdepotModel.strategyList];
        [_tableView reloadData];
        [_tableView.mj_header endRefreshing];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"解析失败");
    }];
}
- (void)creatMJRefreshFoot {
    NSString *urlStr = [NSString stringWithFormat:@"http://api.guju.com.cn/v2/strategy/strategyList?userId=0&count=10&start=%ld&orderString=CATEGORY&category=%@", (long)(_page += 10), _Category];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:urlStr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = [NSDictionary dictionaryWithDictionary:responseObject];
        self.mapdepotModel = [MapDepotModel modelWithDic:dic];
        [_arr addObjectsFromArray:_mapdepotModel.strategyList];
        [_tableView reloadData];
        [_tableView.mj_footer endRefreshing];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"解析失败");
    }];
}
- (void)creatCycleData {
    NSString *urlStr = @"http://api.guju.com.cn/v2/strategy/banner";
    dispatch_queue_t concurrent = dispatch_queue_create("concurrent", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(concurrent, ^{
        [NetworkHandler getDataByUrlString:urlStr BodyString:nil WithDataBlock:^(id result) {
            NSDictionary *dic = result;
            self.cycleModel = [MapCycleModel modelWithDic:dic];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self creatCycleView];
            });
        }];
    });
}
- (void)creatCycleView {
    NSMutableArray *sourceArray = [NSMutableArray array];
    for (CycleBanners *banners in _cycleModel.banners) {
        [sourceArray addObject:banners.phoneImgUrl];
    }
    WYScrollView *cycleView = [[WYScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height / 3) WithNetImages:sourceArray];
    [cycleView setAutoScrollDelay:2];
    _tableView.tableHeaderView = cycleView;
}
- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, self.view.bounds.size.width, self.view.bounds.size.height)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView registerNib:[UINib nibWithNibName:@"MapDepotTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"cell"];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    MapWebViewController *web = [[MapWebViewController alloc] init];
    web.urlStr = [_mapdepotModel.strategyList[indexPath.row].Map_id description];
    web.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:web animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.view.bounds.size.height / 6;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MapDepotTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.strategyList = _arr[indexPath.row];
    return cell;
}
- (void)clickNavLeftBtn:(UIBarButtonItem *)leftBtn {
    LoginLeftViewController *vc = [[LoginLeftViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - JSDropDownMenu协议
- (NSInteger)numberOfColumnsInMenu:(JSDropDownMenu *)menu {
    return 3;
}
- (BOOL)displayByCollectionViewInColumn:(NSInteger)column {
    return YES;
}
- (BOOL)haveRightTableViewInColumn:(NSInteger)column {
    return NO;
}
- (CGFloat)widthRatioOfLeftColumn:(NSInteger)column {
    return 1;
}
- (NSInteger)currentLeftSelectedRow:(NSInteger)column {
    
    return 0;
}
- (NSInteger)menu:(JSDropDownMenu *)menu numberOfRowsInColumn:(NSInteger)column leftOrRight:(NSInteger)leftOrRight leftRow:(NSInteger)leftRow {
    if (column == 0) {
        return _cycleModel.type.before.count;
    } else if (column == 1) {
        return  _cycleModel.type.inArr.count;
    } else {
        return  _cycleModel.type.after.count;
    }
    
}
- (NSString *)menu:(JSDropDownMenu *)menu titleForColumn:(NSInteger)column {
    return _titleArr[column];
}
// 赋值
- (NSString *)menu:(JSDropDownMenu *)menu titleForRowAtIndexPath:(JSIndexPath *)indexPath {
    
    if (indexPath.column==0) {
        return _cycleModel.type.before[indexPath.row].name;
    } else if (indexPath.column == 1) {
        return _cycleModel.type.inArr[indexPath.row].name;
    } else {
        return _cycleModel.type.after[indexPath.row].name;
    }
    
}
// 接口
- (void)menu:(JSDropDownMenu *)menu didSelectRowAtIndexPath:(JSIndexPath *)indexPath {
    if (indexPath.column==0) {
        _Category = _cycleModel.type.before[indexPath.row].before_id;
    } else if (indexPath.column == 1) {
        _Category = _cycleModel.type.inArr[indexPath.row].in_id;
        
    } else {
        _Category = _cycleModel.type.after[indexPath.row].after_id;
    }
    [_tableView.mj_header beginRefreshing];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
